//
//  WYCFastCspController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/12/26.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCFastCspController.h"
#import "WYCBillModel.h"
#import "WYCCardlistPickerView.h"

@interface WYCFastCspController ()
{
        UIButton * cardbutton;
        
        NSString * str;
        UIButton * daybutton;
    
        UILabel * backLabel;
    
}
/** 数组*/
@property(nonatomic,strong)NSMutableArray * billarray;


@property (nonatomic,weak) WYCCardlistPickerView *PickerView;

@property (nonatomic,weak) UILabel *name;
@property (nonatomic,weak) UILabel *bank;

/** 金额*/
@property(nonatomic,strong)UITextField * moneyField;

@end

@implementation WYCFastCspController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"智能消费";
    
    self.view.backgroundColor = WYCGlobalBg;
    
    self.billarray = [[NSMutableArray alloc] init];
    
    //获取信用卡列表
    [self setupCreditcardlist];
    
    
    [self setupCreat];
}

- (void)setupCreditcardlist{
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",creditcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                model.bank = [NSString stringWithFormat:@"%@(%@)",model.bankName,model.lastNumber];
                [self.billarray addObject:model];
            }
            
        }else{
            [self showToast:@""];
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)setupCreat {
    /*** 姓名 ****/
    
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, NavHeight, ScreenWidth, 360 * px);
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    //姓名
    UILabel * cardLabel = [[UILabel alloc] init];
    cardLabel.frame = CGRectMake(40 * px, WYCClearW, 500 * px, WYCClearH);
    cardLabel.text = @"信用卡号后四位:";
    cardLabel.font = MiddleLight;
    [backView addSubview:cardLabel];
    
    //赋值控件
    UILabel * card = [[UILabel alloc] init];
    card.frame = CGRectMake(CGRectGetMaxX(cardLabel.frame) + WYCClearW, cardLabel.y, ScreenWidth / 2, cardLabel.height);
    card.font = forLight;
    [backView addSubview:card];
    
    
    cardbutton = [[UIButton alloc] init];
    cardbutton.frame = CGRectMake(backView.width / 2, cardLabel.y, backView.width / 2 - 80 * px, cardLabel.height);
    [cardbutton setTitle:@"请选择信用卡 >" forState:UIControlStateNormal];
    cardbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [cardbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    cardbutton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [cardbutton addTarget:self action:@selector(cardButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cardbutton];
    
    
    UIView * cardview = [[UIView alloc] init];
    cardview.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(cardLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    cardview.backgroundColor = WYCgrayColor;
    [backView addSubview:cardview];
    
    //姓名
    UILabel * moneyLabel = [[UILabel alloc] init];
    moneyLabel.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(cardview.frame) + WYCClearW , cardLabel.width, WYCClearH);
    moneyLabel.text = @"消费金额:";
    moneyLabel.font = forLight;
    [backView addSubview:moneyLabel];
    
    
    //消费金额
    UITextField * moneyField = [[UITextField alloc] init];
    moneyField.frame = CGRectMake(CGRectGetMaxX(moneyLabel.frame), moneyLabel.y, backView.width - moneyLabel.width - 120 * px, moneyLabel.height);
    moneyField.textAlignment = NSTextAlignmentRight;
    moneyField.keyboardType = UIKeyboardTypeNumberPad;
    [IWNotificationCenter addObserver:self selector:@selector(textFields) name:UITextFieldTextDidChangeNotification object:moneyField];
//    moneyField.text = @"消费金额:";
    moneyField.font = forLight;
    [backView addSubview:moneyField];
    self.moneyField = moneyField;
    
    
    backLabel = [[UILabel alloc] init];
    backLabel.textAlignment = NSTextAlignmentLeft;
    //    backLabel.backgroundColor = [UIColor redColor];
    backLabel.text = @"总计手续费：--";
    backLabel.font = [UIFont systemFontOfSize:WyzFont];
    backLabel.textColor = [UIColor grayColor];
    
    backLabel.frame = CGRectMake(40 * px, CGRectGetMaxY(backView.frame) + 40 * px, ScreenWidth - 80 * px, 48 * px);
    [self.view addSubview:backLabel];
    
    
    UIButton * implementButton = [[UIButton alloc] init];
    implementButton.frame = CGRectMake(40 * px, CGRectGetMaxY(backLabel.frame) + 80 * px, ScreenWidth - 80 * px, 150 * px);
    implementButton.layer.cornerRadius = 10 * px;
    implementButton.layer.masksToBounds = YES;
    implementButton.backgroundColor =  [UIColor redColor];
    [implementButton setTitle:@"执行智能消费" forState:UIControlStateNormal];
    [implementButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [implementButton addTarget:self action:@selector(impleButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:implementButton];
}
- (void)textFields{
    WYCLogFunc;
    double a = [_moneyField.text doubleValue];
    double b = [[WYCAccountTool unarchiverepaymentRate] doubleValue];
    
     double string = a * b / 100;
    backLabel.text = [NSString stringWithFormat:@"总计手续费：%.2f元",string];
}
- (void)cardButtonClick
{
    
    WYCLogFunc;
    if (!self.PickerView) {
        
        NSMutableArray *arr = self.billarray;
        WYCCardlistPickerView *PickerView = [[WYCCardlistPickerView alloc]initWithSourceData:arr confirmBlock:^(WYCBillModel * BillModel) {
            
            NSString * Str = [NSString stringWithFormat:@"%@(%@)>",BillModel.bankName,BillModel.lastNumber];
            [cardbutton setTitle:Str forState:UIControlStateNormal];
//            self.name.text = BillModel.name;
            self.bank.text = BillModel.bankId;
            
            str = BillModel.cardId;
            [self pickerViewhide:YES];
            
            
        } cancelBlock:^(WYCBillModel * BillModel) {
            
            [self pickerViewhide:YES];
            
        }];
        PickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 220);
        [self.view addSubview:PickerView];
        self.PickerView = PickerView;
        [self pickerViewhide:NO];
    }
}

- (void)pickerViewhide:(BOOL)hide {
    
    if (hide) {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.PickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 280);
        }];
    }else {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.PickerView.frame = CGRectMake(0, ScreenHeight - 280, ScreenWidth, 280);
        }];
    }
    
}

- (void)impleButtonClick{
    WYCLogFunc;
    if (self.moneyField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入金额"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"cardId"] = self.name.text;
    params[@"money"] = self.moneyField.text;
    params[@"token"] = [WYCAccountTool unarchivetoken];
    [HttpTool POST:creditcardcardtocash params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"=----------%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            [self.navigationController popViewControllerAnimated:NO];
        }else{
            
             [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    }];
    
    
}

//点击空白推荐盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.moneyField resignFirstResponder];
}


@end
